'use strict';

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller ResourceController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class ResourceController extends Controller {
  async list() {
    /**
        * @summary resource列表分页。
        * @description resource列表分页。
        * @router get /resourcelist
        * @request query integer limit
        * @request query integer page
        */
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.resource.list(params);
    this.ctx.body = {
      data: result.rows,
      msg: '',
      count: result.count,
    };
  }
  async listAll() {
    /**
       * @summary resource列表
       * @description resource列表
       * @router get /resourcelistall
       */
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    const result = await this.ctx.service.resource.listAll();
    this.ctx.body = {
      data: result,
    };
  }
  async create() {
    /**
    * @summary resourcecreate
    * @description resourcecreate
    * @router post /resourcecreate
    * @request body resourcecreate value 传入参数
    */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.resource.create(params);
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async delete() {
    /**
    * @summary resourcedelete
    * @description resourcedelete
    * @router get /resourcedelete
    * @request query string id 传入参数
    */
    const params = this.ctx.query;
    const result = await this.ctx.service.resource.delete(params);
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async edit() {
    const params = this.ctx.request.body;
    const result = await this.ctx.service.resource.edit(params);
    this.ctx.body = {
      msg: result.msg,
    };
  }
}

module.exports = ResourceController;
